import 'package:finance_app/src/entities/bill_detail_entity.dart';
import 'package:finance_app/src/entities/business_contact_entity.dart';
import 'package:finance_app/src/entities/format/service_and_product_entity.dart';
import 'package:finance_app/src/entities/ocr_attachment_entity.dart';
import 'package:finance_app/src/entities/product_entity.dart';
import 'package:finance_app/src/entities/services_entity.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';

billDetailEntityFromJson(BillDetailEntity data, Map<String, dynamic> json) {
	if (json['data_type'] != null) {
		data.dataType = json['data_type'].toString();
	}
	if (json['id'] != null) {
		data.id = json['id'] is String
				? int.tryParse(json['id'])
				: json['id'].toInt();
	}
	if (json['company_id'] != null) {
		data.companyId = json['company_id'] is String
				? int.tryParse(json['company_id'])
				: json['company_id'].toInt();
	}
	if (json['purchase_order_id'] != null) {
		data.purchaseOrderId = json['purchase_order_id'] is String
				? int.tryParse(json['purchase_order_id'])
				: json['purchase_order_id'].toInt();
	}
	if (json['contact_id'] != null) {
		data.contactId = json['contact_id'] is String
				? int.tryParse(json['contact_id'])
				: json['contact_id'].toInt();
	}
	if (json['contact'] != null) {
		data.contact = BusinessContactEntity().fromJson(json['contact']);
	}
	if (json['category_id'] != null) {
		data.categoryId = json['category_id'] is String
				? int.tryParse(json['category_id'])
				: json['category_id'].toInt();
	}
	if (json['category'] != null) {
		data.category = SettingDataCategory().fromJson(json['category']);
	}
	if (json['number'] != null) {
		data.number = json['number'].toString();
	}
	if (json['purchase_order_number'] != null) {
		data.purchaseOrderNumber = json['purchase_order_number'].toString();
	}
	if (json['issued_at'] != null) {
		data.issuedAt = json['issued_at'].toString();
	}
	if (json['due_at'] != null) {
		data.dueAt = json['due_at'].toString();
	}
	if (json['currency_code'] != null) {
		data.currencyCode = json['currency_code'].toString();
	}
	if (json['subtotal'] != null) {
		data.subtotal = json['subtotal'] is String
				? num.tryParse(json['subtotal'])
				: json['subtotal'];
	}
	if (json['exclusive_tax_amount'] != null) {
		data.exclusiveTaxAmount = json['exclusive_tax_amount'] is String
				? num.tryParse(json['exclusive_tax_amount'])
				: json['exclusive_tax_amount'];
	}
	if (json['inclusive_tax_amount'] != null) {
		data.inclusiveTaxAmount = json['inclusive_tax_amount'] is String
				? num.tryParse(json['inclusive_tax_amount'])
				: json['inclusive_tax_amount'];
	}
	if (json['discount_type'] != null) {
		data.discountType = json['discount_type'].toString();
	}
	if (json['discount_amount'] != null) {
		data.discountAmount = json['discount_amount'] is String
				? num.tryParse(json['discount_amount'])
				: json['discount_amount'];
	}
	if (json['credit_used'] != null) {
		data.creditUsed = json['credit_used'] is String
				? num.tryParse(json['credit_used'])
				: json['credit_used'];
	}
	if (json['discount_value'] != null) {
		data.discountValue = json['discount_value'].toString();
	}
	if (json['total'] != null) {
		data.total = json['total'] is String
				? num.tryParse(json['total'])
				: json['total'];
	}
	if (json['items'] != null) {
		data.items = (json['items'] as List).map((v) => BillDetailItems().fromJson(v)).toList();
	}
	if (json['notes'] != null) {
		data.notes = json['notes'];
	}
	if (json['status'] != null) {
		data.status = json['status'].toString();
	}
	if (json['created_by'] != null) {
		data.createdBy = json['created_by'];
	}
	if (json['created_at'] != null) {
		data.createdAt = json['created_at'].toString();
	}
	if (json['updated_at'] != null) {
		data.updatedAt = json['updated_at'].toString();
	}
	if (json['attachments'] != null) {
		data.attachments = (json['attachments'] as List).map((v) => OcrAttachmentEntity().fromJson(v)).toList();
	}
	if (json['is_overdue'] != null) {
		data.isOverdue = json['is_overdue'] is String
				? int.tryParse(json['is_overdue'])
				: json['is_overdue'].toInt();
	}
	if (json['delivered_at'] != null) {
		data.deliveredAt = json['delivered_at'].toString();
	}
	if (json['unpaid'] != null) {
		data.unpaid = json['unpaid'] is String
				? num.tryParse(json['unpaid'])
				: json['unpaid'];
	}
	return data;
}

Map<String, dynamic> billDetailEntityToJson(BillDetailEntity entity) {
	final Map<String, dynamic> data = new Map<String, dynamic>();
	data['data_type'] = entity.dataType;
	data['id'] = entity.id;
	data['company_id'] = entity.companyId;
	data['purchase_order_id'] = entity.purchaseOrderId;
	data['contact_id'] = entity.contactId;
	data['contact'] = entity.contact?.toJson();
	data['category_id'] = entity.categoryId;
	data['category'] = entity.category?.toJson();
	data['number'] = entity.number;
	data['purchase_order_number'] = entity.purchaseOrderNumber;
	data['issued_at'] = entity.issuedAt;
	data['due_at'] = entity.dueAt;
	data['currency_code'] = entity.currencyCode;
	data['subtotal'] = entity.subtotal;
	data['exclusive_tax_amount'] = entity.exclusiveTaxAmount;
	data['inclusive_tax_amount'] = entity.inclusiveTaxAmount;
	data['discount_type'] = entity.discountType;
	data['discount_amount'] = entity.discountAmount;
	data['credit_used'] = entity.creditUsed;
	data['discount_value'] = entity.discountValue;
	data['total'] = entity.total;
	data['items'] =  entity.items?.map((v) => v.toJson())?.toList();
	data['notes'] = entity.notes;
	data['status'] = entity.status;
	data['created_by'] = entity.createdBy;
	data['created_at'] = entity.createdAt;
	data['updated_at'] = entity.updatedAt;
	data['attachments'] =  entity.attachments?.map((v) => v.toJson())?.toList();
	data['is_overdue'] = entity.isOverdue;
	data['delivered_at'] = entity.deliveredAt;
	data['unpaid'] = entity.unpaid;
	return data;
}

billDetailItemsFromJson(BillDetailItems data, Map<String, dynamic> json) {
	if (json['data_type'] != null) {
		data.dataType = json['data_type'].toString();
	}
	if (json['id'] != null) {
		data.id = json['id'] is String
				? int.tryParse(json['id'])
				: json['id'].toInt();
	}
	if (json['company_id'] != null) {
		data.companyId = json['company_id'] is String
				? int.tryParse(json['company_id'])
				: json['company_id'].toInt();
	}
	if (json['bill_id'] != null) {
		data.billId = json['bill_id'] is String
				? int.tryParse(json['bill_id'])
				: json['bill_id'].toInt();
	}
	if (json['type'] != null) {
		data.type = json['type'].toString();
	}
	if (json['name'] != null) {
		data.name = json['name'].toString();
	}
	if (json['quantity'] != null) {
		data.quantity = json['quantity'] is String
				? int.tryParse(json['quantity'])
				: json['quantity'].toInt();
	}
	if (json['currency_code'] != null) {
		data.currencyCode = json['currency_code'].toString();
	}
	if (json['tax_id'] != null) {
		data.taxId = json['tax_id'] is String
				? int.tryParse(json['tax_id'])
				: json['tax_id'].toInt();
	}
	if (json['type_id'] != null) {
		data.typeId = json['type_id'] is String
				? int.tryParse(json['type_id'])
				: json['type_id'].toInt();
	}
	if (json['tax'] != null) {
		data.tax = SettingDataTax().fromJson(json['tax']);
	}
	if (json['price'] != null) {
		data.price = json['price'] is String
				? num.tryParse(json['price'])
				: json['price'];
	}
	if (json['nett_price'] != null) {
		data.nettPrice = json['nett_price'] is String
				? num.tryParse(json['nett_price'])
				: json['nett_price'];
	}
	if (json['is_tax_inclusive'] != null) {
		data.isTaxInclusive = json['is_tax_inclusive'] is String
				? int.tryParse(json['is_tax_inclusive'])
				: json['is_tax_inclusive'].toInt();
	}
	if (json['tax_amount'] != null) {
		data.taxAmount = json['tax_amount'] is String
				? num.tryParse(json['tax_amount'])
				: json['tax_amount'];
	}
	if (json['created_at'] != null) {
		data.createdAt = json['created_at'].toString();
	}
	if (json['updated_at'] != null) {
		data.updatedAt = json['updated_at'].toString();
	}
	if (json['discount_type'] != null) {
		data.discountType = json['discount_type'].toString();
	}
	if (json['discount_value'] != null) {
		data.discountValue = json['discount_value'] is String
				? num.tryParse(json['discount_value'])
				: json['discount_value'];
	}
	return data;
}

Map<String, dynamic> billDetailItemsToJson(BillDetailItems entity) {
	final Map<String, dynamic> data = new Map<String, dynamic>();
	data['data_type'] = entity.dataType;
	data['id'] = entity.id;
	data['company_id'] = entity.companyId;
	data['bill_id'] = entity.billId;
	data['type'] = entity.type;
	data['name'] = entity.name;
	data['quantity'] = entity.quantity;
	data['currency_code'] = entity.currencyCode;
	data['tax_id'] = entity.taxId;
	data['type_id'] = entity.typeId;
	data['tax'] = entity.tax?.toJson();
	data['price'] = entity.price;
	data['nett_price'] = entity.nettPrice;
	data['is_tax_inclusive'] = entity.isTaxInclusive;
	data['tax_amount'] = entity.taxAmount;
	data['created_at'] = entity.createdAt;
	data['updated_at'] = entity.updatedAt;
	data['discount_type'] = entity.discountType;
	data['discount_value'] = entity.discountValue;
	return data;
}